/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.iboss.service.sc;

import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.iboss.dao.sc.SCBussinessDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SCBussinessService extends BaseService {
    @Autowired
    private SCBussinessDao scBussinessDao;

    public List<Map> listBussAllProOrderByMap(Map map) {
        List<Map> mainList = scBussinessDao.listBussAllMainOrderByMap(map);//查询所有主订单
        String ids = "";
        for (Map<String, Object> mainId : mainList) {
            for (Map.Entry<String, Object> vo : mainId.entrySet()) {
                ids = ids + (vo.getValue()) + ",";//拼接主订单IDS
            }
        }
        if (ids != "") {
            ids = ids.substring(0, ids.length() - 1);
        }
        map.put("ids", ids);
        List<Map> scmap = scBussinessDao.listBussAllProOrderByMap(map);//查询主订单对应的子订单
        List<Map> numMap = scBussinessDao.queryRowspanNum(map);//获取每个主订单id对应的子订单数量
        for (Map mapTemp : scmap) {
            String orderId = String.valueOf(mapTemp.get("mainid"));
            for (Map numMapTemp : numMap) {
                String numOrderId = String.valueOf(numMapTemp.get("mainid"));
                if (!numMapTemp.containsKey("flag") && orderId.equals(numOrderId)) {
                    mapTemp.put("rowspanl", String.valueOf(numMapTemp.get("rowspan")));
                    numMapTemp.put("flag", 1);
                } else if (numMapTemp.containsKey("flag") && orderId.equals(numOrderId)) {
                    mapTemp.put("rowspanl", 0);
                }
            }

        }
        return scmap;
    }

    public Integer countListBussAllMainOrderByMap(Map map) {
        return scBussinessDao.countListBussAllMainOrderByMap(map);
    }
}
